package org.bigdata.framework.core.controller;

import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSONObject;
import net.sf.json.JSONArray;

/**
 * 基础控制层
 * @author vimes
 *
 */
public class BaseController {

	protected void pringWriterToPage(String content,
			HttpServletResponse response) throws Exception {
		this.pringWriterToPage(content, null, response);
	}

	protected void pringWriterToPage(String content, String contentType,
			HttpServletResponse response) throws Exception {
		// "application/json"
		// ".txt"="text/plain"
		// "text/xml"
		if (contentType != null && !"".equals(contentType)) {
			response.setContentType(contentType);
		} else {
			// response.setContentType("text/plain");
		}
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter pw = response.getWriter();
		pw.print(content);
		pw.flush();
		pw.close();
	}

	protected String formatToJson(Object obj) {
		JSONArray jsonObject = JSONArray.fromObject(obj);
		try {
			// System.out.println(jsonObject.toString());
			return jsonObject.toString();
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	
	protected String toJsonString(Object obj) {
		return JSONObject.toJSONString(obj);
	}
}
